﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;

namespace FloatingWindowTools
{
    public class MapinfoWrapper
    {
        private IMapInfo2 mapinfo;
        public MapinfoWrapper(IMapInfo2 mapInfo)
        {
            this.mapinfo = mapInfo;
        }

        public static MapinfoWrapper CreateMapinfo(string pathtomiadmdll)
        {
            Assembly miadmassembly = Assembly.LoadFrom(Path.Combine(pathtomiadmdll,"miadm.dll"));

            Type intertype = miadmassembly.GetType("MapInfo.MiPro.Interop.InteropServices");
            //object interobject = Activator.CreateInstance(intertype);

            PropertyInfo propery = intertype.GetProperty("MapInfoApplication", BindingFlags.Public | BindingFlags.Static);

            object obj = null;
            object value = propery.GetValue(obj, null);

            IMapInfo2 mapinfo = value.GetPrivateFieldValue<IMapInfo2>("_mapinfo");
            return new MapinfoWrapper(mapinfo);
        }

        public static MapinfoWrapper CreateMapinfo(Assembly miadmassembly)
        {
            Type intertype = miadmassembly.GetType("MapInfo.MiPro.Interop.InteropServices");
            //object interobject = Activator.CreateInstance(intertype);

            PropertyInfo propery = intertype.GetProperty("MapInfoApplication", BindingFlags.Public | BindingFlags.Static);

            object obj = null;
            object value = propery.GetValue(obj, null);

            IMapInfo2 mapinfo = value.GetPrivateFieldValue<IMapInfo2>("_mapinfo");
            return new MapinfoWrapper(mapinfo);
        }

        public void Do(string cmd)
        {
            this.mapinfo.Do(cmd);
        }

        public string Eval(string cmd)
        {
            return this.mapinfo.Eval(cmd);
        }
    }
}
